<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Banner extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
		$this->load->library('tank_auth');
		//$this->lang->load('tank_auth');
        if(!$this->tank_auth->is_logged_in())
            redirect('administrator/auth');
        $this->load->model('banners');
        $this->load->model('categories','cat');
    }

    public function index($act = false){
        $post= $this->input->post(null);
        
        $banners = $this->banners->getBanners();
        $categories = $this->cat->getCat();
        $this->assign('categories',$categories);
        $this->assign('banners',$banners);
        $this->assign('action',$this->uri->segment(4));
        $this->assign('banner_id',$this->uri->segment(5));
        if($act)
        {   if($act=="save"){
                $config['max_size'] ='100M';
                        $config['upload_path'] = 'cdn/uploads/'. strtoupper(dechex(sprintf("%u", crc32(date('Y-m-d'))))).'/';
                        $config['allow_type'] = array('jpg','gif','png');
                switch ($post['action']) {
                    case 'create':
                    
                        if(!empty($_FILES)){
                            foreach($_FILES as $key=>$val){
                                
                               $dt = _upload($config,$key);
                               if($dt)
                                    $data[] = $dt['url'];
                               else
                               {
                                    if(empty($images))
                                        $flag = false;
                               }
                            }
                        }
                        $this->banners->addBanner(array('name'=>$post['name'],'ordering'=>$post['ordering'],'link'=>$post['link'],'position'=>$post['parent_id'],'image'=>$data[0]));
                    break;
                    case 'edit':
                    $flag = true;
                        if(!empty($_FILES)){
                            
                               $dt = _upload($config);
                              
                               if($dt){
                                    $data = $dt['url'];
                               @unlink(base_url().$post['img']);
                                }else{
                                    $flag = false;
                                }
                            
                        }
                        if(!$flag)
                            $data = $post['img'];
                       
                        $this->banners->update_banner(array('name'=>$post['name'],'ordering'=>$post['ordering'],'link'=>$post['link'],'position'=>$post['parent_id'],'image'=>$data),array('id'=>$post['banner_id']));
                        
                    break;
                }
                redirect('administrator/banner/index');
            }
            elseif($act=="delete"){
                
            }elseif($act=="edit"){
                $banner = $this->banners->getBanners(array('id'=>$this->uri->segment(5)));
                $this->assign('banner',$banner);
            }elseif($act=="create"){
                //@TODO
            }
            else show_404();
            
        }
    }
    /**
     * Default function
     */
    

}